/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package config;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;

/**
 *
 * @author RYUDO
 */
public class Function {
    /** Check the connection file is existed or not **/
    public static boolean check_connection_file() {
        File f = new File("DB.con");
        if(!f.exists()) {
            return false;
        } else {
            return true;
        }
    }
    /*
     * This function uses to align the windows
     */
    public static void Windows_centered(int w,int h,Object input) {
        if(input instanceof JFrame) {
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            int x = (screen.width-w)/2;
            int y = (screen.height-h)/2;
            ((JFrame)input).setBounds(x, y, w, h);
        } else if(input instanceof JDialog) {
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            int x = (screen.width-w)/2;
            int y = (screen.height-h)/2;
            ((JDialog)input).setBounds(x, y, w, h);
        }
    }
    /*
     * Loading effect
     */
    public static void loadEffect() throws InterruptedException {
        Thread.sleep(1000);
    }

     /** Clear all the text field in the form **/
    public static void resetComponents(JFrame input1,JPanel input2) {
        Component[] cp;
        if(input1 != null) {
            cp = input1.getContentPane().getComponents();
            for(int i = 0;i<cp.length;i++) {
                if(cp[i] instanceof JTextComponent) {
                    ((JTextComponent)cp[i]).setText(null);
                } else if(cp[i] instanceof JTextArea) {
                    ((JTextArea)cp[i]).setText(null);
                }
            }
        } else
        if(input2 != null) {
            cp = input2.getComponents();
           for(int i = 0;i<cp.length;i++) {
                if(cp[i] instanceof JTextComponent) {
                    ((JTextComponent)cp[i]).setText(null);
                } else if(cp[i] instanceof JTextArea) {
                    ((JTextArea)cp[i]).setText(null);
                }
            }
        }
    }

    /*
     * Convert the gender from database
     */
    public static String get_gender(String input) {
        if(input.equals("1")) {
            return "Male";
        } else {
            return "Female";
        }
    }
    /*
     * Set the gender from database
     */
    public static String set_gender(String input) {
        if(input.equals("Male")) {
            return "1";
        } else {
            return "0";
        }
    }
    /*
     * Convert the date to the specified pattern
     */
    public static String date_converter(String pattern,Date input) {
        DateFormat df = new SimpleDateFormat(pattern);
        String date = df.format(input);
        return date;
    }
   
    

  
}
